﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Text;
using System.Windows.Forms;

using System.IO;
using DoxygenBrowserAddin.Controller;
using DoxygenBrowserAddin.SecurityZones;


namespace DoxygenBrowserAddin.Views
{
    /// <summary>
    /// 
    /// </summary>
    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    [ComVisible(true)]
    public partial class DoxygenBrowser : UserControl
    {
        private DoxAddinController _controller;
        private Timer _statusBarTimer;

        public void SetController(DoxAddinController controller)
        {
            _controller = controller;
            _controller.DoxygenConfigChanged += new EventHandler(_controller_DoxygenConfigChanged);
            enableNavigationControls();
            _statusBarTimer = new Timer();
            _statusBarTimer.Tick += new EventHandler(_statusBarTimer_Tick);
            _statusBarTimer.Interval = 3000;


        }

        void _controller_DoxygenConfigChanged(object sender, EventArgs e)
        {
            _homePath = _controller.GetDoxHtmlDirectory();
        }

        private void enableNavigationControls()
        {
            _btnGoForward.Enabled = _webBrowser.CanGoForward;
            _btnGoBack.Enabled = _webBrowser.CanGoBack;
        }

        string _homePath;
        public string HomePath
        {
            get { return _homePath; }
            set
            {
                _homePath = value;
                NavigateHome();
            }
        }

        public void NavigateHome()
        {
            bool bHasHomePath = !string.IsNullOrEmpty(_homePath);
            if (bHasHomePath)
            {
                string strHomePage = getHomePage();
                string strPath = Path.Combine(_homePath, strHomePage);
                _webBrowser.Navigate(strPath);
            }
            else
            {
                _webBrowser.DocumentText = "<html><body>" +
                 "Solution is not loaded" +
               "</body></html>";

            }
            this.Enabled = bHasHomePath;
        }

        private string getHomePage()
        {
            string strHomePage = "index.html";

            if (_controller.DoxygenConfig.UseFrames)
            {
                strHomePage = "main.html";
            }
            return strHomePage;
        }

        public void SetStatusMessage(string statusMessage)
        {
            _tssLblStatus.Text = statusMessage;
            _statusBarTimer.Stop();
            _statusBarTimer.Start();
        }

        void _statusBarTimer_Tick(object sender, EventArgs e)
        {
            _statusBarTimer.Stop();
            _tssLblStatus.Text = string.Empty;
        }

        public void StartIndexing(string filePath)
        {
            SetStatusMessage("Indexing " + filePath);
            _tsProgressBar.Value = 0;
            _tsProgressBar.Visible = true;
        }

        public void ProgressIndexing(int value)
        {
            _tsProgressBar.Value = value;
        }

        public void EndIndexing(string filePath)
        {
            SetStatusMessage("Indexed " + filePath);

            _tsProgressBar.Visible = false;
        }

        public DoxygenBrowser()
        {
            InitializeComponent();
            setInternetSecurityManager(_webBrowser);
            _webBrowser.CanGoBackChanged += new EventHandler(_webBrowser_CanGoBackChanged);
            _webBrowser.CanGoForwardChanged += new EventHandler(_webBrowser_CanGoForwardChanged);
        }

        private void setInternetSecurityManager(WebBrowser webBrowser)
        {
            object obj = webBrowser.ActiveXInstance;

            ZoneTestCOM.IServiceProvider sp = obj as ZoneTestCOM.IServiceProvider;
            object ops;
            sp.QueryService(ref ZoneTestCOM.SID_SProfferService, ref ZoneTestCOM.IID_IProfferService, out ops);
            ZoneTestCOM.IProfferService ps = ops as ZoneTestCOM.IProfferService;
            int cookie = 0;

            InternetSecurityManager manager = new InternetSecurityManager();

            ps.ProfferService(ref ZoneTestCOM.IID_IInternetSecurityManager, manager, ref cookie);

        }

        public void Navigate(string strURL)
        {
            _webBrowser.Navigate((string)strURL);
        }

        void _webBrowser_CanGoForwardChanged(object sender, EventArgs e)
        {
            _btnGoForward.Enabled = _webBrowser.CanGoForward;
        }

        void _webBrowser_CanGoBackChanged(object sender, EventArgs e)
        {
            _btnGoBack.Enabled = _webBrowser.CanGoBack;
        }

        private void _btnGoBack_Click(object sender, EventArgs e)
        {
            _webBrowser.GoBack();
        }

        private void _btnGoForward_Click(object sender, EventArgs e)
        {
            _webBrowser.GoForward();
        }

        private void _btnHome_Click(object sender, EventArgs e)
        {
            NavigateHome();
        }


    }
}
